package com.sx.pipeline.ui.examine.record

import android.os.Bundle
import android.view.View
import androidx.annotation.NonNull
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.CollectionUtils
import com.hjq.base.BaseAdapter.OnItemClickListener
import com.lxj.xpopup.XPopup
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.sx.pipeline.R
import com.sx.pipeline.adapter.ExamineRecordAdapter
import com.sx.pipeline.app.base.CYDataBindingBaseFragment
import com.sx.pipeline.app.config.IntentKey
import com.sx.pipeline.arouth.RouteManager
import com.sx.pipeline.databinding.FragExamineTaskBinding
import com.sx.pipeline.repos.http.res.ExamineOperatorVo
import com.sx.pipeline.repos.http.res.ExamineRecordVo

class ExamineRecordPageFrag:
    CYDataBindingBaseFragment<FragExamineTaskBinding, ExamineRecordVM>(R.layout.frag_examine_task),
    View.OnClickListener  {

    private var status = 0 //任务状态
    var mAdapter = ExamineRecordAdapter()

    companion object {
        fun newInstance(position: Int) =
            ExamineRecordPageFrag().apply {
                arguments = Bundle().apply {
                    putInt(IntentKey.ID, position)
                }
            }
    }

    override fun initData() {
        status = arguments?.getInt(IntentKey.ID)!!
        mBinding.recyclerView.layoutManager=LinearLayoutManager(context)
        mBinding.recyclerView.adapter=mAdapter
        mViewModel.getExamineList(mPage,status)
        mAdapter.setOnItemClickListener{adapter, view, position ->
            val bean = mAdapter.getItem(position)
            if(view.id==R.id.tv_again){
                //重新提交
                RouteManager.Main.intentToJoinBusiness()
            }else if(view.id==R.id.tv_cancel){
                //取消申请
                XPopup.Builder(context)
                    .asConfirm("提示","确定要取消申请吗？"){
                        mViewModel.cancelApply(bean)
                    }.show()
            }else{
                RouteManager.Mine.intentToExaminePerson(bean.id.toString(),false)
            }
        }
    }

    override fun initListener() {
        super.initListener()
        mBinding.refreshLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {

            override fun onLoadMore(@NonNull refreshLayout: RefreshLayout) {
                mPage++
                mViewModel.getExamineList(mPage,status)
            }

            override fun onRefresh(@NonNull refreshLayout: RefreshLayout) {
                mPage = 1
                mViewModel.getExamineList(mPage,status)
            }
        })
    }

    override fun onClick(p0: View?) {
    }

    override fun initViewObservable() {
        super.initViewObservable()
        // 刷新事件
        observe(mViewModel.mUI.initRefreshEvent()) {
            mBinding.refreshLayout.finishRefresh(it!!)
        }

        // 加载事件
        observe(mViewModel.mUI.initLoadEvent()) {
            mBinding.refreshLayout.finishLoadMore(it!!)
        }

        observe(mViewModel.examineListEvent){
            if (mPage == 1) {
                mAdapter.setNewInstance(it?.records as MutableList<ExamineRecordVo>)
                if(CollectionUtils.isEmpty(it?.records)){
                    mBinding.layoutEmpty.visibility=View.VISIBLE
                }else{
                    mBinding.layoutEmpty.visibility=View.GONE
                }
            } else {
                it?.let { it1 -> mAdapter.addData(it1.records) }
            }
        }

        observe(mViewModel.refreshEvent){
            mPage=1
            mViewModel.getExamineList(mPage,status)
        }

    }
}